﻿//if (window.location.href.indexOf('?') > window.location.href.indexOf('#')) {
//    var r = window.location.href.match(/#[^\?]*/g);
//    if (r) {
//        window.location.href = window.location.href.replace(r[0], '') + r[0];
//    }
//}

var myApp = angular.module("CrmApp", ["ngRoute", "ngRestUrls", "ngResource"]).config(["$provide", "$sceProvider", "$compileProvider", "$httpProvider", "$routeProvider", "$restUrls", function ($provide, $sceProvider, $compileProvider, $httpProvider, $routeProvider, $restUrls) {
    
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|file|blob):|wxlocalresource:\//);
    var v = '?v=' + Math.random();
    //用户中心
    $routeProvider.when('/usercenter', {
        templateUrl: "partials/user_center.html",
        controller: "UserCenterCtrl"
    }).when('/sign', {
        templateUrl: "partials/sign.html",
        controller: "SignCtrl"
    }).when('/ecshop', {
        templateUrl: "partials/ecshop.html",
        controller: "ShopCtrl"
    }).when('/mypoints', {
        templateUrl: "partials/mypoints.html",
        controller: "UserCenterCtrl"
    }).when('/mygroup', {
        templateUrl: "partials/mygroup.html",
        controller: "UserCenterCtrl"
    }).when('/productlist', {
        templateUrl: "partials/product_list.html",
        controller: "ProductListCtrl"
    }).when('/product/:id', {
        templateUrl: "partials/product.html",
        controller: "ProductCtrl"
    }).when('/userinfo', {
        templateUrl: "partials/userinfo.html",
        controller: "UserInfoCtrl"
    }).when('/account', {
        templateUrl: "partials/account.html",
        controller: "AccountCtrl"
    }).when('/lottery', {
        templateUrl: "partials/lottery.html",
        controller: "LotteryCtrl"
    }).when('/settlement/:id', {
        templateUrl: "partials/settlement.html",
        controller: "SettlementCtrl"
    }).when('/add_address', {
        templateUrl: "partials/add_address.html",
        controller: "Add_AddressCtrl"
    }).when('/addresslist', {
        templateUrl: "partials/address_list.html",
        controller: "AddressListCtrl"
    }).when('/orderlist', {
        templateUrl: "partials/order_list.html",
        controller: "OrderListCtrl"
    }).when('/orderdetail/:id', {
        templateUrl: "partials/order_detail.html",
        controller: "OrderCtrl"
    }).when('/logistics/:id', {
        templateUrl: "partials/logistics.html",
        controller: "LogisticsCtrl"
    }).when('/coupon', {
        templateUrl: "partials/customer_coupon.html",
        controller: "CouponCtrl"
    }).when('/userprofile', {
        templateUrl: "partials/customer_profile.html",
        controller: "CustomerProfileCtrl"
    }).when('/addreview/:id', {
        templateUrl: "partials/add_review.html",
        controller: "AddReviewCtrl"
    })
    //.when('/login', {
    //    templateUrl: "partials/login.html",
    //    controller: "LoginCtrl"
    //})
    .when('/bindchild', {
        templateUrl: "partials/bind_child.html",
        controller: "ChildCtrl"
    }).when('/register', {
        templateUrl: "partials/register.html",
        controller: "RegisterCtrl"
    }) .when('/mycoupon', {
        templateUrl: "partials/mycoupon.html",
        controller: "CouponCtrl"
    }).when('/receivelottery/:id', {
        templateUrl: "partials/receive_lottery_result.html",
        controller: "ReceiveLotteryCtrl"
    }).when('/rss', {
        templateUrl: "partials/rss.html",
        controller: "RSSCtrl"
    }).when('/activity', {
        templateUrl: "partials/activity.html",
        controller: "activity_controller"
    }).when('/rss_detail/:id', {
        templateUrl: "partials/rssdetails.html",
        controller: "RSSDetailCtrl"
    }).when('/news/', {
        templateUrl: "partials/newslist.html",
        controller: "NewsCtrl"
    }).when('/news_detail/:id', {
        templateUrl: "partials/newsdetails.html",
        controller: "NewsDetailCtrl"
    }).when('/familyinfo', {
        templateUrl: "partials/familyinfo.html",
        controller: "FamilyCtrl"
    }).when('/childinfo/:sort', {
        templateUrl: "partials/childinfo.html",
        controller: "ChildCtrl"
    }).when('/recommendinfo', {
        templateUrl: "partials/recommendinfo.html",
        controller: "RecommendUserCtrl"
    }).when('/integrallist', {
        templateUrl: "partials/integral_list.html",
        controller: "IntegralCtrl"
    }).when('/questions', {
        templateUrl: "partials/questions.html",
        controller: "QuestionsCtrl"
    }).when('/number', {
        templateUrl: "partials/number.html",
        controller: "NumberCtrl"
    }).when('/step', {
        templateUrl: "partials/step.html",
        controller: "StepCtrl"
    })
    .otherwise({
        redirectTo: "/usercenter"
    })


    $httpProvider.interceptors.push(function () {
        return {
            'response': function (response) {
                if (response && typeof response.data === 'object') {
                    if (response.data.success == 3) {
                        
                        var source_url = window.location.href.match(/#[^\?]*/g)[0].replace("#", '');
                        var redirect_uri = encodeURIComponent("http://educrm.anoah.com/api/index.php?route=account/social/weixin_auth_callback&source_url=" + source_url);
                        var weixin_auth_2url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx19b5657da7a8db9e&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                        window.location.href = weixin_auth_2url;
                    }
                }
                return response || $q.when(response);
            }
        };
    });

    $provide.provider("$post", {
        $get: ['$http', '$httpParamSerializer', function ($http, $httpParamSerializer) {
            function $post(url, data, config) {
                return $http(angular.extend(config || {}, { url: url, method: 'POST', data: $httpParamSerializer(data), headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }))
            };
            return $post;
        }]
    });
    $sceProvider.enabled(false);

}]);

myApp.filter('urldecode', function ($sce) {
    return function (text) {
        return decodeURIComponent(text);
    }
});

myApp.filter('nullToEmpty', function ($sce) {
    return function (text) {
        return text ? text : '';
    }
});



myApp.filter('int', function ($sce) {
    return function (text) {
        return parseInt(text);
    }
});

myApp.filter('trusted', ['$sce', function ($sce) {
    var div = document.createElement('div');
    return function (text) {
        div.innerHTML = text;
        return $sce.trustAsHtml(div.textContent);
    };
}]);

myApp.controller('rootController', ['$rootScope', '$scope', '$http', '$routeParams', '$location', '$restUrls', function ($rootScope, $scope, $http, $routeParams, $location, $restUrls) {
     $scope.getUrlParam =function(name) {
        var reg = new RegExp("([\?&]{0,1})" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.href.match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    //微信接口初始化
     var InitWeiXinConfig = function () {
        var url = window.location.href.replace(/#[^\?]*/g, '');
        $http({ method: "GET", url: $restUrls["weiXinSetting"], params: { url: encodeURIComponent(url) } }).success(function (result) {
            if (result.success == 1) {
                var data = result.data;
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: data.wx_appId, // 必填，公众号的唯一标识
                    timestamp: data.wx_timestamp, // 必填，生成签名的时间戳
                    nonceStr: data.wx_nonceStr, // 必填，生成签名的随机串
                    signature: data.wx_signature,// 必填，签名，见附录1
                    jsApiList: ['chooseWXPay', 'chooseImage', 'uploadImage', 'previewImage', 'scanQRCode', 'getLocation', 'previewImage', 'onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                });
            } else {
                $.scojs_message('微信接口初始化失败', $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('微信接口初始化失败', $.scojs_message.TYPE_ERROR);
        });
     }
     $scope.ConverToInt = function (strings) {
         return Number(strings);
     }

     InitWeiXinConfig();

}])



